const fs = require('fs');
const BaseIpcHandler = require('../BaseIpcHandler');

class LoadImageHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> LoadImageHandler params = ', params);
        const { image_path } = params;

        // 业务逻辑 && 返回结果
        if (event.from === 'localserver') {
            event.resp.sendFile(image_path, (err) => {
                if (err) {
                    console.error('Error sending file:', err);
                    event.resp.status(404).send('File not found');
                }
            });
        } else {
            const result = fs.readFileSync(image_path);
            return result;
        }
    }
}

// 测试读取本地文件的解决方案
// let image = nativeImage.createFromPath(msg);
// let imageBase64 = image.toDataURL();
// return imageBase64;

// const image = nativeImage.createFromPath(msg);
// let img = image.toPNG();

module.exports = LoadImageHandler;
